package com.cskaoyan.service.impl;

import com.cskaoyan.mapper.AftersaleMapper;
import com.cskaoyan.mapper.OrderGoodsMapper;
import com.cskaoyan.mapper.OrderMapper;
import com.cskaoyan.mapper.UserMapper;
import com.cskaoyan.model.*;
import com.cskaoyan.model.bo.AftersaleBO;
import com.cskaoyan.model.vo.WxDetailVO;
import com.cskaoyan.service.AftersaleService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @description：TODO 售后模块
 * @author     ：huzhouming
 * @date       ：2022/5/11 11:41
 */
@Service
public class AftersaleServiceImpl implements AftersaleService {
    @Autowired
    AftersaleMapper aftersaleMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    OrderMapper orderMapper;
    @Autowired
    OrderGoodsMapper orderGoodsMapper;

    @Override
    /**
     * @author: huzhouming
     * @description: TODO 生成售后订单
     * @date: 2022/5/11
     * @param aftersaleBO
     * @return void
     */
    public void insertAftersale(AftersaleBO aftersaleBO) {

        Aftersale aftersale = new Aftersale(aftersaleBO);
        Subject subject = SecurityUtils.getSubject();
        String username = (String) subject.getPrincipals().getPrimaryPrincipal();
        Integer integer = userMapper.selectUserIdByUsername(username);
        aftersale.setUserId(integer);
        Order order = orderMapper.selectByPrimaryKey(aftersaleBO.getOrderId());
        //与订单编号共用
        aftersale.setAftersaleSn(order.getOrderSn());
        aftersaleMapper.insert(aftersale);
        //修改订单信息
        order.setOrderStatus((short) 202);
        order.setAftersaleStatus((short)1);
        order.setRefundAmount(new BigDecimal(aftersaleBO.getAmount()));
        order.setRefundType(aftersaleBO.getTypeDesc());
        order.setRefundContent(aftersaleBO.getReason());
        order.setUpdateTime(new Date());
        OrderExample orderExample = new OrderExample();
        OrderExample.Criteria criteria = orderExample.createCriteria();
        criteria.andIdEqualTo(order.getId());
        order.setId(null);
        orderMapper.updateByExampleSelective(order,orderExample);
    }

    @Override
    public WxDetailVO getDetail(Integer orderId) {
        Order order = orderMapper.selectByPrimaryKey(orderId);
        List<OrderGoods> orderGoods = orderGoodsMapper.selectOrderGoodsByOrderId(orderId);
        AftersaleExample aftersaleExample = new AftersaleExample();
        AftersaleExample.Criteria criteria = aftersaleExample.createCriteria();
        criteria.andOrderIdEqualTo(orderId);
        List<Aftersale> aftersales = aftersaleMapper.selectByExample(aftersaleExample);
        WxDetailVO wxDetailVO = new WxDetailVO();
        wxDetailVO.setAftersale(aftersales.get(0));
        wxDetailVO.setOrder(order);
        wxDetailVO.setOrderGoods(orderGoods);
        return wxDetailVO;
    }
}
